package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.Container;
import com.example.springboot.entity.vo.ContainerVO;
import com.example.springboot.service.PickUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: jyf2000jyf
 * @Date: 2022/6/5
 * @Time: 23:09
 * @Email: 904593011@qq.com
 * @Description:
 **/
@RestController
@RequestMapping("pickUp")
public class PickUpController {

    @Autowired
    PickUpService pickUpService;

    @GetMapping("page")
    public Result indexByPage(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam String shipOrder) {
        pageNum = (pageNum - 1) * pageSize;
        List<Container> data;
        Integer total;
        if (shipOrder == null || "".equals(shipOrder)) {
            data = pickUpService.findAll(pageNum, pageSize, Integer.valueOf(0));
            total = pickUpService.getAmount(Integer.valueOf(0));
        } else {
            data = pickUpService.findAll(pageNum, pageSize, Integer.valueOf(shipOrder));
            total = pickUpService.getAmount(Integer.valueOf(shipOrder));
        }
        return Result.success(new ContainerVO(data, total));
    }

    @PostMapping
    public Result save(@RequestBody Container container) {
        // 新增或者更新
        return Result.success(pickUpService.save(container));
    }

    @GetMapping("user")
    public Result saveUserId(@RequestParam Integer id,
                             @RequestParam String username) {
        return Result.success(pickUpService.saveUserId(id,username));
    }
}
